package com.weige.conver.handler;

import com.alibaba.fastjson.JSONObject;
import com.weige.conver.config.ActivitiConfig;
import com.weige.conver.entity.EnvData;
import com.weige.conver.entity.ReceiveMessage;
import com.weige.conver.service.DeviceService;
import com.weige.conver.service.EnvDataService;
import com.weige.conver.service.RabbitMQService;
import lombok.SneakyThrows;

import java.io.IOException;

/**
 * <p>Title: TimeControlDaysCodeHandler</p>
 * <p>Description: </p>
 *  时控天数代码转物模型
 * @author chenchunxu
 * @version 1.0.0
 * @date 2021/11/18 14:16
 */
public class TimeControlDaysCodeHandler implements ConverHandler {

    RabbitMQService rabbitMQService = ActivitiConfig.getBean(RabbitMQService.class);
    DeviceService deviceService = ActivitiConfig.getBean(DeviceService.class);
    EnvDataService envDataService = ActivitiConfig.getBean(EnvDataService.class);

    @SneakyThrows
    @Override
    public void handler(ReceiveMessage receiveMessage) throws IOException {
        String deviceId = receiveMessage.getDevId();
        String deviceName = receiveMessage.getDevName();
        //物联网平台数据配置
        String model = "time_control_days_code";
        String manufactureId = "ShuyanyuanAIGateway";
        String serviceName = "envDataService";
        EnvData env = envDataService.getEnvDataByData(receiveMessage,model,"tcdc","时控天数代码值","时控天数代码测量值","");
        //判断数据对象是否为空，并上传到RabbitMQ
        if (env != null){
            String data = JSONObject.toJSONString(env);
            rabbitMQService.sendData(data,deviceId,deviceName,model,manufactureId,serviceName);
        }
    }
}
